#include"game.h"

//初始化棋盘
void InitBoard(char board[ROW][COL],int row,int col)

{
    int i=0,j=0;
    for ( i = 0; i < ROW; i++)
    {
        for ( j = 0; j < COL; j++)
        {
            board[i][j]=' ';
        }   
    }
}


//打印棋盘
void PlayBoard(char board[ROW][COL],int row,int col)

{
    //打印数据
    int i=0;
    for ( i = 0; i < row; i++)
    {
        int j=0;
        for ( j = 0; j < col; j++)
        {
            printf(" %c ",board[i][j]);
            if(j<col-1)
            printf("|");
        }
        printf("\n");
        if (i<row-1)
        {
            for ( j = 0; j < col; j++)
            {
                printf("---");
                if(j<col-1)
                printf("|");
            }
        printf("\n");         
        }   
    }
}

//玩家下棋
void PlayerMove(char board[ROW][COL],int row,int col)

{
    int x=0,y=0;
    
    while (1)
    {
        printf("请玩家输入坐标(横坐标与纵坐标以空格隔开):\n");
        scanf("%d %d",&x,&y);
        if(x<=row&&y<=col)
        {
            if (board[x-1][y-1]==' ')
            {
                board[x-1][y-1]='*';
                break;
            } 
            else;
        }
        else 
        {
            printf("坐标输入错误，请重新输入：\n");
        }
    }      
}

//电脑下棋
void ComputerMove(char board[ROW][COL],int row,int col)

{
    printf("电脑输入：\n");
    int x=0,y=0;
    
    while(1)
    {
        x= rand()%ROW;
        y= rand()%COL;
        if (board[x][y]==' ')
        {
            board[x][y]='#';
            break;
        }
    } 
}


//判断胜负
int Win(char board[ROW][COL],int row,int col)

{
    int i=0;
    for(i = 0;i < COL;i++)  
    {
        int j=0,n=0,m=0,k=0;
        //判断行相等
        while(board[i][0]==board[i][j+1]&&j<=COL-2&&board[i][0]!=' ') 
        {
            if (j==COL-2&&board[i][0]=='*')
            {
                return 1;
            }
            else if (j==COL-2&&board[i][0]=='#')
            {
                return 2;
            }
            j++;
        }

        //判断列相等  
        while (board[0][i]==board[n+1][i]&&n<=ROW-2&&board[0][i]!=' ')
        {
            if (n==ROW-2&&board[0][i]=='*')
            {
                return 1;
            }
            else if (n==ROW-2&&board[0][i]=='#')
            {
                return 2;
            }
            n++;
        }

        //判断对角相等
        while (board[0][0]==board[m+1][m+1]&&m<=ROW-2&&board[0][0]!=' ')
        {
            if (m==ROW-2&&board[0][0]=='*')
            {
                return 1;
            }
            else if (m==ROW-2&&board[0][0]=='#')
            {
                return 2;
            }
            m++;
        }
        while (board[0][COL-1]==board[k+1][COL-k-2]&&k<=ROW-2&&board[0][COL-1]!=' ')
        {
            
            if (k==COL-2&&board[0][COL-1]=='*')
            {
                return 1;
            }
            if (k==COL-2&&board[0][COL-1]=='#')
            {
                return 2;
            }
            
            k++;
        }
        
    }
    return 0;
}